<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace im_cs\driver;

/**
 * Description of TimApi
 *
 * @author SoChishun
 * @version 2018-4-13
 */
class QcloudTIM {

    protected $api;

    public function __construct() {
        require_once env('root_path') . 'extend/tim/TimRestAPI.php';
        $this->api = new \TimRestAPI();
        $this->api->init(config('sdkaccount.tim_sdkappid'), config('sdkaccount.tim_identifier'));
    }

    /**
     * 
     * @param type $id
     * @return type
     * @version 2018-4-12
     */
    function getTimSig($id) {
        // 2018-4-12
        // $id不建议直接使用user_id，而应该是加密后的user_id,防止秘钥泄露
        // identifier长度建议不超过 32 字节 
        // https://cloud.tencent.com/document/product/269/1508 (文档首页   云通信   账号登录集成   独立模式)
        // https://cloud.tencent.com/document/product/269/1510#1-.E6.A6.82.E8.BF.B0 TLS后台API使用手册
        // TLS 后台 API 默认接口生成的签名有效期为 180 天，开发者可以使用含有有效期参数的接口自行设定有效期，开发者需要在签名过期前到开发者后台获取新的签名
        // 开发者请注意命令执行的路径和可执行权限,注意 PHP 环境需要带 openssl 扩展，否则接口使用会报错，另外只支持 PHP 5.3 及以上的版本。
        $tim_private_key_path = env('root_path') . config('sdkaccount.tim_private_key_path');
        if (!file_exists($tim_private_key_path)) {
            return fmt_result(false, '私钥文件不存在:' . $tim_private_key_path);
        }
        if ($id == '') {
            return fmt_result(false, '参数id不能为空');
        }
        if (intval(9223372036854775807) > 0) {
            if (PATH_SEPARATOR == ':') {
                $signature = "signature/linux-signature64";
            } else {
                $signature = "signature\\windows-signature64.exe";
            }
        } else {
            if (PATH_SEPARATOR == ':') {
                $signature = "signature/linux-signature32";
            } else {
                $signature = "signature\\windows-signature32.exe";
            }
        }
        # 这里需要写绝对路径，开发者根据自己的路径进行调整
        $tool_path = env('root_path') . "extend/tim/" . $signature;
        $command = implode(' ', [escapeshellarg($tool_path), escapeshellarg($tim_private_key_path), escapeshellarg(config('sdkaccount.tim_sdkappid')), escapeshellarg($id)]);
        $ret = exec($command, $out, $status);
        if ($status == -1) {
            return fmt_result(false, '生成签名失败!');
        }
        return fmt_result(true, $out[0]);
    }

    /**
     * 独立模式账号导入接口
     * https://cloud.tencent.com/document/product/269/1608
     * @param type $id 用户名，长度不超过 32 字节
     * @param type $nick_name
     * @param type $face_url
     * @param type $type 帐号类型，开发者默认无需填写，值0表示普通帐号，1表示机器人帐号。
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0}
     * @version 2018-4-13
     */
    public function accountImport($id, $nick_name, $face_url, $type = 0) {
        return $this->api->account_import($identifier, $nick, $face_url);
    }

    /**
     * 单发单聊消息
     * https://cloud.tencent.com/document/product/269/2282
     * @param type $account_id
     * @param type $receiver
     * @param type $message
     * @return array {"ActionStatus":"OK",   "ErrorInfo":"",   "ErrorCode":0,"MsgTime":1497238162}
     */
    public function sendMsg($account_id, $receiver, $message) {
        $msg_content = [
            "MsgType" => "TIMTextElem",
            "MsgContent" => [
                "Text" => $message
            ]
        ];
        $this->api->openim_send_msg2($account_id, $receiver, $msg_content);
    }
    /**
     * 批量发单聊消息
     * https://cloud.tencent.com/document/product/269/1612
     * @param type $receiver_list
     * @param type $message
     * @return array {"ActionStatus":"FAIL","ErrorInfo":"","ErrorList":[{"To_Account":"rong","ErrorCode":20003}]}
     */
    public function batchsendmsg($receiver_list,$message){
        $msg_content = [
            "MsgType" => "TIMTextElem",
            "MsgContent" => [
                "Text" => $message
            ]
        ];
        return $this->api->openim_batch_sendmsg2($receiver_list, $msg_content);
        
    }
    /**
     * 获取APP中的所有群组
     * https://cloud.tencent.com/document/product/269/1614
     * @param type $limit
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0,"TotalCount":2,"GroupIdList":[{"GroupId":"@TGS#2J4SZEAEL"},{"GroupId":"@TGS#2C5SZEAEF"}]，"Next":4454685361}
     */
    public function getGroupList($limit=50){
        return $this->api->group_get_appid_group_list2($limit, null, null);
        
    }
    /**
     * 创建群组
     * https://cloud.tencent.com/document/product/269/1615
     * @param type $group_type 群组类型：私有群Private/公开群Public/聊天室ChatRoom/互动直播聊天室AVChatRoom/在线成员广播大群BChatRoom（必填）
     * @param type $group_name 群名称，最长30字节
     * @param type $owner_id 群主id，自动添加到群成员中。如果不填，群没有群主。
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0,"GroupId":"@TGS#2J4SZEAEL"}
     */
    public function createGroup($group_type, $group_name, $owner_id){
        return $this->api->group_create_group($group_type, $group_name, $owner_id);
    }
    
    /**
     * 增加群组成员
     * https://cloud.tencent.com/document/product/269/1621
     * 私有群、公开群和聊天室支持使用本REST API增加群成员；
     * 互动直播聊天室和在线成员广播大群不支持增加群成员，对这两种类型的群组进行操作时会返回10007错误。用户加入这两种群组的唯一方式是用户申请加群。
     * @param type $group_id
     * @param type $member_id
     * @param type $silence 是否静默加人。0：非静默加人；1：静默加人。不填该字段默认为0。当Silence为1时，成员添加成功后，不会给任何人下发系统通知。
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0,"MemberList":[{"Member_Account":"tommy","Result":1},{"Member_Account":"jared","Result":1}]}
     */
    public function addGroupMember($group_id, $member_id, $silence){
        return $this->api->group_add_group_member($group_id, $member_id, $silence);
    }
    /**
     * 删除群组成员
     * https://cloud.tencent.com/document/product/269/1622
     * @param type $group_id
     * @param type $member_id
     * @param type $silence 是否静默删人。0：非静默删人；1：静默删人。不填该字段默认为0。
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0}
     */
    public function deleteGroupMember($group_id, $member_id, $silence){
        return $this->api->group_delete_group_member($group_id, $member_id, $silence);
    }
    /**
     * 在群组中发送普通消息
     * https://cloud.tencent.com/document/product/269/1629
     * @param type $account_id
     * @param type $group_id
     * @param type $text_content
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0,"MsgTime":1497249503,"MsgSeq":1}
     */
    public function sendGroupMsg($account_id, $group_id, $text_content){
        return $this->api->group_send_group_msg($account_id, $group_id, $text_content);
    }
    
    /**
     * 在群组中发送系统通知
     * https://cloud.tencent.com/document/product/269/1630
     * @param type $group_id
     * @param type $text_content
     * @param type $receiver_id 接收者群成员列表，不填或为空表示全员下发。
     * @return array {"ActionStatus":"OK","ErrorInfo":"","ErrorCode":0}
     */
    public function sendGroupSystemNotification($group_id, $text_content, $receiver_id=null){
        return $this->api->group_send_group_system_notification($group_id, $text_content, $receiver_id);
    }
    // 设置全局禁言 https://cloud.tencent.com/document/product/269/4230
    public function setNoSpeaking(){
        
    }

}
